<link rel="stylesheet" type="text/css" href="../static/Huploadify-master/Huploadify.css">
<script type="text/javascript" src="../static/Huploadify-master/jquery.Huploadify.js"></script>
<style>
    li {
        list-style-type: none;
        vertical-align: middle;
        height: 30px;
        line-height: 30px;
        margin: 10px 0;
    }

    li a {
        text-decoration: none;
    }

    li a img {
        border: 1px solid #999999;
        margin-right: 20px;
        float: left
    }

    li a div {
        float: left;
        width: 50%;
        text-align: center;
        height: 30px;
        line-height: 30px;
        vertical-align: middle;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    li a div {
        width: 50%;
        text-align: center;
    }

</style>
<script type="text/javascript">
    $(function () {
        var keyId = (getUrlParam("keyId", url));
        fy_api.load_template({

            controller: $("#saveForm").attr("templateParam", "keyId=" + keyId),
            loading: true,
            initValue: true,
            loaded: function () {
                fileUploadInit();
                if (keyId != "") {
                    //编辑
                    loadFileList();
                } else {
                    //添加
                    $("#divEdit").remove();
                    $("#saveForm").attr("action", "advertise/save.do")
                }
                loaded();
                init();
            }
        });
    });

    function init() {
        $("#btnSave").click(function () {
            if (!checkForm(document.getElementById("saveForm"))) {
                return;
            }
            var adUrl = $("#advertiseUrl").val();
            if (adUrl.length > 0) {
                //判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
                var expressionURL = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
                var objExp = new RegExp(expressionURL);
                if (objExp.test(adUrl) != true) {
                    addError("advertiseUrl", "网址格式不正确");
                    return;
                }
            }
            if (undefined == $("#attachment").find("li") || $("#attachment").find("li").length == 0) {
                addError($("#fileUpload"), "请上传图片.");
                return;
            }
            $("#btnSave").blur();
            showLoading();
            var imgurl = "";
            $("#attachment").find("li").each(function () {
                var tempurl = $(this).attr("id");
                imgurl += tempurl + ",";
            });
            if ("" != imgurl && imgurl.length > 2) {
                imgurl = imgurl.substr(0, imgurl.length - 1);
                $("#imgUrl").val(imgurl);
            }
            var fileInputs = $("#saveForm").find("input[type='file']").attr("type", "hidden");
            fy_api.ajaxSubmit($("#saveForm"), {
                success: function (data) {
                    fileInputs.attr("type", "file");
                    hideLoading();
                    if (!isSuccess(data)) {
                        $.messager.alert('系统提示', data.msg, 'error', function () {
                            self.focus();
                        });
                    } else {
                        parent.grid_get("grid").datagrid("load");
                        turnBack();
                    }
                }
            });
        });
    }

    function turnBack() {
        parent.j_closeWin();
    }

    //金融代码
    function loaded() {
        $('#company').inputgrid({
            idField: 'o.code',
            textField: 'o.name',
            searchField: ['o.code', 'o.name'],//查询字段多字段匹配
            columns: [[
                {field: 'o.code', title: '金融代码'},
                {field: 'o.name', title: '名称'},
                {field: 'o.address', title: '地址'}
            ]],
            _page: parent.page_list,
            _append: [],//隐藏列
            _table: "company as o",
            _db: "slave"
        });
    }

    //上传构件
    function fileUploadInit() {
        //定义上传文件类型
        var fileUploadType = "uploadfile";
        //保存文件到服务器上的路径
        var fileSavePath = saveBasePathInServer + "advertise/images/";
        $('#fileUpload').Huploadify({
            auto: false,
            fileTypeExts: '*.jpg;*.png;*.jpge;*.bmp;*.gif',
            multi: false,
            formData: {savePath: fileSavePath},
            fileSizeLimit: 10200,
            showUploadedPercent: false,
            showUploadedSize: false,
            removeTimeout: 1000,
            uploader: uploadServer + fileUploadType + "&savePath=" + fileSavePath,
            method: 'post',
            onUploadStart: function (file) {
            },
            onInit: function (obj) {
            },
            onUploadSuccess: function (file, data) {
                data = eval("(" + data + ")");
                if (data.state == "SUCCESS") {
                    var fileName = file.name;
                    var fileUrl = lookingServer + data.url;
                    //如果是图片文件则显示图片，否则显示文件的连接
                    if (data.type == ".zip") {
                        $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                            "<a target='_blank' href='" + fileUrl + "'>" +
                            "<img src='../../../static/images/zipPic.png' height='30px' width='30px' style='margin-right:20px;' />" +
                            "<div>" + fileName + "</div>" +
                            "</a>" +
                            "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                            "</li>");
                    } else {
                        $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                            "<a target='_blank' href='" + fileUrl + "'>" +
                            "<img src='" + fileUrl + "?_t=" + Math.random() + "' height='30px' width='30px' style='margin-right:20px;' />" +
                            "<div>" + fileName + "</div>" +
                            "</a>" +
                            "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                            "</li>");
                    }
                    if ($("#attachment").find("li").length > 1) {
                        $("#attachment").children().first().remove();
                    }
                } else {
                    j_tip("[" + file.name + "]" + data.state, "error");
                }
            },
            onUploadComplete: function (file) {
            },
            onCancel: function (file) {
            },
            onClearQueue: function (queueItemCount) {
            },
            onDestroy: function () {
            },
            onSelect: function (file) {
            },
            onQueueComplete: function (queueData) {
            }
        });
    }

    //图片鼠标移入事件
    function imgOver(obj) {
        $(obj).children("span").show();
    }

    //图片鼠标移出事件
    function imgOut(obj) {
        $(obj).children("span").hide();
    }

    //删除图片
    function imgDel(obj) {
        if (confirm("确定删除此图片吗?", "提示")) {
            $(obj).parent().remove();
        }
    }

    //加载附件
    function loadFileList() {
        var fileUrlsArr = new Array();
        var fileUrls = $("#imgUrl").val();
        if (fileUrls.length > 0) {
            for (var i = 0; i < fileUrls.split(",").length; i++) {
                var fileUrl = fileUrls.split(",")[i];
                if ("" != fileUrl && undefined != fileUrl) {
                    var fileName = fileUrl.substr(fileUrl.lastIndexOf("/") + 1, fileUrl.length);
                }
                if (fileUrl.substr(fileUrl.length - 4) == ".zip") {
                    $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                        "<a target='_blank' href='" + fileUrl + "'>" +
                        "<img src='../../../static/images/zipPic.png' height='30px' width='30px' style='margin-right:20px;' />" +
                        "<div>" + fileName + "</div>" +
                        "</a>" +
                        "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                        "</li>");
                } else {
                    $("#attachment").append("<li id='" + fileUrl + "' title='" + fileName + "'>" +
                        "<a target='_blank' href='" + fileUrl + "'>" +
                        "<img src='" + fileUrl + "?_t=" + Math.random() + "' height='30px' width='30px' style='margin-right:20px;' />" +
                        "<div>" + fileName + "</div>" +
                        "</a>" +
                        "<span class='button-link icon-config-delete' title='删除文件' onclick='imgDel(this)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" +
                        "</li>");
                }
            }
        }
    }

</script>
<form action="advertise/update.do" method="post" id="saveForm" templateUrl="advertise/get.do">
    <div id="divEdit">
        <input type="hidden" name="id" value="${content.id}"/>
    </div>
    <table style="margin: 10px auto;border: 1px solid darkgrey;padding: 10px">
        <tr>
            <td>
                <div class="div-label"><span class="nullTip">*</span>内容：</div>
            </td>
            <td colspan="3">
                <div class="div-item" style="width: 90%">
                    <input style="width: 93%" type="text" isNull name="content" value="${content.content}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">金融代码：</div>
            </td>
            <td>
                <div class="div-item">
                    <input name="code.code" type="hidden" id="code" value="${content.code.code}" clear/>
                    <input type="text" name="companyName" id="company" value="${content.companyName}"/>
                </div>
            </td>
            <td>
                <div class="div-label"><span class="nullTip">*</span>持续时间(天)：</div>
            </td>
            <td>
                <div class="div-item">
                    <input type="text" name="duration" isInt numType="1" value="${content.duration}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label"><span class="nullTip">*</span>是否启用：</div>
            </td>
            <td>
                <div class="div-item">
                    <select name="deleteFlag" id="deleteFlag" style="width: 160px;" value="${content.deleteFlag}">
                        <option value="0">启用</option>
                        <option value="1">禁用</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="div-label"><span class="nullTip">*</span>是否主页显示：</div>
            </td>
            <td>
                <div class="div-item">
                    <select name="isHomePage" id="isHomePage" style="width: 160px;" value="${content.isHomePage}">
                        <option value="0">不显示</option>
                        <option value="1">显示</option>
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">备注：</div>
            </td>
            <td colspan="3">
                <div class="div-item" style="width: 90%">
                    <input type="text" name="remark" value="${content.remark}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label">广告URL：</div>
            </td>
            <td colspan="3">
                <div class="div-item" style="width: 90%">
                    <input type="text" name="advertiseUrl" id="advertiseUrl" value="${content.advertiseUrl}"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="div-label"><span class="nullTip">*</span>图片：</div>
            </td>
            <td>
                <input type="hidden" id="imgUrl" name="imgUrl" value="${content.imgUrl}"/>
                <div id="fileUpload"></div>
            </td>
            <td colspan="2">
                <ul id="attachment" style="padding:5px"></ul>
            </td>
        </tr>
    </table>
</form>
<div class="form-buttons">
    <a id="btnSave" class="easyui-linkbutton" iconCls="icon-save">保存</a>
    <a id="btnBack" class="easyui-linkbutton c2" iconCls="icon-undo" onClick="turnBack();">返回</a>
</div>